//计算二叉树的深度
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
    int ret = 0;

public:
    void dfs(TreeNode* root, int count) {
        if (root == nullptr)
            return;
        ret = max(count, ret);
        dfs(root->left, count + 1);
        dfs(root->right, count + 1);
    }
    int calculateDepth(TreeNode* root) {
        // if (root == nullptr)
        //   return 0;
        dfs(root, 1);
        return ret;
    }
};

//单值二叉树
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
    int ret;
    bool f = true;

public:
    void dfs(TreeNode* root) {
        if (root == nullptr)
            return;
        if (ret != root->val) {
            f = false;
            return;
        }
        dfs(root->left);
        dfs(root->right);
        return;
    }
    bool isUnivalTree(TreeNode* root) {
        ret = root->val;
        dfs(root);
        return f;
    }
};